package thread;

/**
 * @User: vitobo
 * @Date: 2024-08-01
 * @Description: 三个线程，分别只能打印A,B,C， 保证这三个线程，固定按照ABC这样的顺序来打印
 *
 *  必须先执行wait, 再执行notify;
 *
 *
 *
 */
public class ThreadDemo18 {

    // t1 打印完之后, 再通知t2; t2打印完之后, 再通知t3打印;
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{
            System.out.println("A");
            synchronized (locker1){
                locker1.notify();
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (locker1){
                try {
                    locker1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("B");
            synchronized (locker2){
                locker2.notify();
            }
        });
        Thread t3 = new Thread(()->{
            synchronized (locker2){
                try {
                    locker2.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("C");
        });


        // 必须先执行wait, 再执行notify;
        t2.start();
        t3.start();
        Thread.sleep(1000);
        t1.start();


    }
}
